<?php 
define("CCTRL", "tourhistory");
define("CCTRLNAME", "tourhistory");
session_start();
class tourhistory extends CI_Controller  {

        /**
        * Index Page for this controller.
        *
        * Maps to the following URL
        * 		http://example.com/index.php/welcome
        *	- or -  
        * 		http://example.com/index.php/welcome/index
        *	- or -
        * Since this controller is set as the default controller in 
        * config/routes.php, it's displayed at http://example.com/
        *
        * So any other public methods not prefixed with an underscore will
        * map to /index.php/welcome/<method_name>
        * @see http://codeigniter.com/user_guide/general/urls.html
        */
       private $_configs = null;
       function __construct()
        {
            parent::__construct();
            $this->load->helper('url');
            $this->load->library('javascript');
            $this->load->library('session');
            $this->load->library('smarty3');
            $this->load->model('admin-planners/tour_model');
            $this->load->model('admin-planners/setting_model');
            include APPPATH . 'libraries/defu.php';
            $this->InitSetting();
            $this->smarty=new CI_Smarty3();
            $this->setting_model=new setting_model();
            $this->tour_model=new tour_model();
            if(!isset($_SESSION["ADP"][CCTRL]["display"]))$_SESSION["ADP"][CCTRL]["display"]=1;
        }
        
        function InitSetting(){
            $colModel=array(
                array(  "display"       =>"ID"                  ,"name"=>"ID"               ,"width"=> 60        ,"sortable"=>true      ,"align"=>"center"      ,"hide"=>false      ,"filter"=>false),
                array(  "display"       =>"Full Name"           ,"name"=>"FullName"         ,"width"=>100        ,"sortable"=>true      ,"align"=>"left"        ,"hide"=>false      ,"filter"=>true),
                array(  "display"       =>"Age"                 ,"name"=>"Age"              ,"width"=> 60       ,"sortable"=>true       ,"align"=>"left"        ,"hide"=>false      ,"filter"=>false ),
                array(  "display"       =>"Num of Pe"           ,"name"=>"Num"              ,"width"=> 60       ,"sortable"=>true       ,"align"=>"left"        ,"hide"=>false      ,"filter"=>false),
                array(  "display"       =>"Date"                ,"name"=>"Date"             ,"width"=> 80       ,"sortable"=>true       ,"align"=>"right"       ,"hide"=>false      ,"filter"=>false),
                array(  "display"       =>"Phone"               ,"name"=>"Phone"            ,"width"=> 80       ,"sortable"=>true       ,"align"=>"right"       ,"hide"=>false      ,"filter"=>false),
                array(  "display"       =>"Location"            ,"name"=>"Location"         ,"width"=>100       ,"sortable"=>true       ,"align"=>"left"        ,"hide"=>false      ,"filter"=>false ),
                array(  "display"       =>"Tour"                ,"name"=>"Title"             ,"width"=>180       ,"sortable"=>true      ,"align"=>"left"        ,"hide"=>false      ,"filter"=>true ),
                array(  "display"       =>"Insert"              ,"name"=>"Insert"           ,"width"=> 80       ,"sortable"=>true       ,"align"=>"left"        ,"hide"=>false      ,"filter"=>false),
                array(  "display"       =>"Update"              ,"name"=>"Update"           ,"width"=> 80       ,"sortable"=>true       ,"align"=>"left"        ,"hide"=>true       ,"filter"=>false),
                array(  "display"       =>"Delete"              ,"name"=>"Delete"           ,"width"=> 80       ,"sortable"=>true       ,"align"=>"left"        ,"hide"=>true       ,"filter"=>false)
            );
            if(isset($_SESSION["ADP"]["USER"])){
                if(isset($_SESSION["ADP"][CCTRL]["colModel"])){
                    $colModel= $_SESSION["ADP"][CCTRL]["colModel"];
                }else{
                    $_SESSION["ADP"][CCTRL]["colModel"]=$colModel;
                }
            }
        }
        public function index()
	{
            if(isset($_SESSION["ADP"]["Filter_".CCTRL]))unset($_SESSION["ADP"]["Filter_".CCTRL]);
            if(!checkAuthority(CCTRLNAME)){
                $this->smarty
                        ->assign('_SESSION', $_SESSION)
                        ->display("sys/04_access_denied");
                return;
            }
            $tours=$this->tour_model->gets();
            foreach ($tours as $t) {
                $Data["filter_value"][]=array("value"=>$t->ID,"display"=>$t->Title);
            }
            $Data["flexigrid_settings"]["colModel"]=$_SESSION["ADP"][CCTRL]["colModel"];
            $Data["flexigrid_settings"]["display"]=$_SESSION["ADP"][CCTRL]["display"];
            $Data["flexigrid_settings"]["filterModel"]=filterModel($_SESSION["ADP"][CCTRL]["colModel"]);
            $this->smarty
                    ->assign('_SESSION', $_SESSION)
                    ->assign('Data', $Data)
                    ->display("admin-planners/".CCTRLNAME."/".CCTRLNAME);
	}
        public function filterbytour($id)
	{
            if(isset($_SESSION["ADP"]["Filter_".CCTRL]))unset($_SESSION["ADP"]["Filter_".CCTRL]);
            if(!isset($_SESSION["ADP"]["USER"])){
                $this->smarty->display("admin-planners/01_login");
                return;
            }
            $_SESSION["ADP"]["Filter_".CCTRL]["Filters"][]=$id;
            $_SESSION["ADP"]["Filter_".CCTRL]["FilterType"]="me";
            $Data["flexigrid_settings"]["filterdefault"]=$_SESSION["ADP"]["Filter_".CCTRL]["Filters"];
            $Data["flexigrid_settings"]["colModel"]=$_SESSION["ADP"][CCTRL]["colModel"];
            $Data["flexigrid_settings"]["display"]=$_SESSION["ADP"][CCTRL]["display"];
            $Data["flexigrid_settings"]["filterModel"]=filterModel($_SESSION["ADP"][CCTRL]["colModel"]);
            $tours=$this->tour_model->gets();
            foreach ($tours as $t) {
                $Data["filter_value"][]=array("value"=>$t->ID,"display"=>$t->Title);
            }
            $this->smarty
                    ->assign('_SESSION', $_SESSION)
                    ->assign('Data', $Data)
                    ->display("admin-planners/tourhistory/tourhistory");
            
	}
        public function ChangeColumnDisplay(){ 
            for($i=0;$i<count($_SESSION["ADP"][CCTRL]["colModel"]);$i++){
                if($_SESSION["ADP"][CCTRL]["colModel"][$i]["name"]==$_POST["col"]){
                    $_SESSION["ADP"][CCTRL]["colModel"][$i]["hide"]=($_POST["hide"]==1?true:false);
                    break;
                }
            }
            $code=1;
            $msg="Data display have been change.";
            echo json_encode(array("code"=>$code,"msg"=>$msg));
        }
        public function ChangeDeleteDisplay(){
            $_SESSION["ADP"][CCTRL]["display"]=$_POST["showDelete"];
            $code=1;
            $msg="Data display have been change.";
            echo json_encode(array("code"=>$code,"msg"=>$msg));
        }
        function FlexiGridData(){
            
            $data=$this->tour_model->FlexiGridData_History();
            header("Content-type: application/json");
            $jsonData = array('page'=>$data["page"],'total'=>$data["total_rows"],'rows'=>array());
            foreach($data["rows"] AS $row){
                    //If cell's elements have named keys, they must match column names
                    //Only cell's with named keys and matching columns are order independent.
                    $cell=array();
                    foreach ($_SESSION["ADP"][CCTRL]["colModel"] as $col){
                        $c=$col["name"];
                        $cell[$col["name"]]=$row->$c;
                        
                    }
                    $cell['Type']=htmlentities_UTF8($row->Type);
                    $cell['Title']=htmlentities_UTF8($row->Title);
                    $id="ID";
                    $entry = array('id'=>$row->$id,
                            'cell'=>$cell
                    );
                    $jsonData['rows'][] = $entry;
            }
            echo json_encode($jsonData);
        }
        
        function Filter(){
            if($_POST["type"]=="me")
                $_SESSION["ADP"]["Filter_".CCTRL]["Filters"]=$_POST["filters"];
            $_SESSION["ADP"]["Filter_".CCTRL]["FilterType"]=$_POST["type"];
            echo json_encode(array("code"=>1,"msg"=>"Filter susess."));
        }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */